package edu.gatech.geochat;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import edu.gatech.geochat.database.Storage;
import edu.gatech.geochat.model.GeoMessage;
import edu.gatech.geochat.model.User;

public class FoundNotesActivity extends BetterActivity {
	public static void launch(Activity source, User user) {
		if (user == null) return;
		Intent intent = new Intent(source, FoundNotesActivity.class);
		source.startActivity(intent);
		GUISpace.put(PlacedNotesActivity.class, "user", user);
	}

	private ListView listsView;
	private FoundAdapter listAdapter;
	private List<GeoMessage> placed;

	@Override
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.found_notes_activity);

		listAdapter = new FoundAdapter();
		listsView = (ListView) this.findViewById(R.layout.note_list_view);
		if (listsView != null)
			listsView.setAdapter(listAdapter);
		placed = Storage.getInstance().getPlaced((User) GUISpace.get(getClass(), "user"));
		setStatusText("");

	}

	private void viewNote(GeoMessage m) {
		ViewNoteActivity.launch(this, m);
	}

	class FoundAdapter extends BaseAdapter {
		private LayoutInflater inflater;

		/**
		 * Adapter constructor that initializes the inflater.
		 */
		public FoundAdapter() {
			inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		/**
		 * Standard android method for the adapter. Returns the amount of items
		 * to display, which is the amount of TaskLists stored by the used
		 * AccountLists.
		 */
		public int getCount() {
			return placed.size();
		}

		/**
		 * Standard android method for the adapter. Returns the item displayed
		 * at the given index.
		 * 
		 * @param i
		 *            the index of the item to be retrieved
		 */
		public GeoMessage getItem(int i) {
			return placed.get(i);
		}

		/**
		 * Return a unique id for the item displayed at that position. This id
		 * should remain the same even as the item changed positions within the
		 * display.
		 * 
		 * @param position
		 *            the hashcode of the item at the given index
		 */
		public long getItemId(int position) {
			return getItem(position).hashCode();
		}

		/**
		 * Standard Android method for Adapter. Called whenever any visual
		 * element being displayed is changed. Returns the View or Edit layout
		 * as appropriate for the TaskList with the given index position.
		 * 
		 * @param position
		 *            the position of the displayed item in the display
		 * @param convert
		 *            a possibly reusable old view
		 * @param parent
		 *            The parent that this view will eventually be attached to
		 */
		public View getView(int position, View convert, ViewGroup parent) {
			final GeoMessage message = placed.get(position);
			if (message != null) {
				View view = inflater.inflate(R.layout.note_list_view, null);
				ImageView deleteView = (ImageView) view.findViewById(R.id.deleteNote);

				String text = message.getText();
				if (text != null) {
					TextView textView = (TextView) view.findViewById(R.id.noteText);
					if (text.length() > 30) {
						text = text.substring(0, 30) + "...";
					}
					OnClickListener loadViewListener = new OnClickListener() {
						public void onClick(View v) {
							viewNote(message);
						}
					};

					textView.setText(text);
					textView.setOnClickListener(loadViewListener);

					deleteView.setVisibility(View.INVISIBLE);
					deleteView.setEnabled(false);

					textView.setText(text);
					textView.setOnClickListener(loadViewListener);

					return textView;
				}

			}
			return null;

		}
	}
}
